﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ProjectStorageLib.Project;

public partial class controls_HideColumn : System.Web.UI.UserControl
{
    #region Vars
    private decimal idProject
    {
        get { return ViewState["idProject"] == null ? decimal.MinValue : (decimal)ViewState["idProject"]; }
        set { ViewState["idProject"] = value; }
    }

    public List<string> hiddenFields
    {
        get { return ViewState["hiddenFields"] == null ? null : (List<string>)ViewState["hiddenFields"]; }
        set { ViewState["hiddenFields"] = value; }
    }

    public delegate void HideField();
    public event HideField HideFieldEvent = null;

    #endregion Vars

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public void LoadData(ProjectManager projectManager)
    {
        if (projectManager != null)
        {
            idProject = projectManager.Project.ProjectID;

            m_rptFactorsCategories.DataSource = projectManager.FactorsCategories;
            m_rptFactorsCategories.DataBind();
            m_rptAgileMethods.DataSource = projectManager.AgileMethods;
            m_rptAgileMethods.DataBind();
        }
        else
        {
            MyAccordion.Visible = false;
        }
    }

    protected void cb_GroupCheckedChanged(object sender, EventArgs e)
    {
        //locals
        ProjectManager pm = null;
        CheckBox cb = (CheckBox)sender; 

        try
        {
            //create instance
            pm = new ProjectManager(idProject);

            //get names of all practice in methods
            List<string> names = pm.AgileMethods.Where(mt => mt.Name == cb.Text).FirstOrDefault().AgilePractices.Select(pr => pr.Name).ToList();

            if (cb.Checked)
            {
                if (hiddenFields == null)
                    hiddenFields = new List<string>();

                foreach (string st in hiddenFields)
                    if (names.Contains(st))
                        names.Remove(st);

                hiddenFields.AddRange(names);
            }
            else
            {
                foreach (string st in names)
                    if (hiddenFields.Contains(st))
                        hiddenFields.Remove(st);
            }

        }
        catch (Exception)
        {
            throw;
        }
        finally
        {
            if (pm != null)
                pm.Dispose();
        }


    }

    protected void cb_GroupFactorCheckedChanged(object sender, EventArgs e)
    {
        //locals
        ProjectManager pm = null;
        CheckBox cb = (CheckBox)sender;

        try
        {
            //create instance
            pm = new ProjectManager(idProject);

            //get names of all practice in methods
            List<string> names = pm.FactorsCategories.Where(mt => mt.Name == cb.Text).FirstOrDefault().Factors.Select(pr => pr.Name).ToList();

            if (cb.Checked)
            {
                if (hiddenFields == null)
                    hiddenFields = new List<string>();

                foreach (string st in hiddenFields)
                    if (names.Contains(st))
                        names.Remove(st);

                hiddenFields.AddRange(names);
            }
            else
            {
                foreach (string st in names)
                    if (hiddenFields.Contains(st))
                        hiddenFields.Remove(st);
            }

        }
        catch (Exception)
        {
            throw;
        }
        finally
        {
            if (pm != null)
                pm.Dispose();
        }
    }

    protected void cb_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox cb = (CheckBox)sender;
        if (cb.Checked)
        {
            if (hiddenFields == null)
                hiddenFields = new List<string>();

            if (!hiddenFields.Contains(cb.Text))
                hiddenFields.Add(cb.Text);
        }
        else
        {
            if (hiddenFields.Contains(cb.Text))
                hiddenFields.Remove(cb.Text);
        }
    }

    protected void bHide_Click(object sender, EventArgs e)
    {
        if (HideFieldEvent != null)
            HideFieldEvent();
    }
}